package com.liuqing.www.phone.server.底层消息;

import java.io.IOException;
import java.net.UnknownHostException;

import org.apache.log4j.Logger;
import org.xsocket.connection.IServer;
import org.xsocket.connection.Server;

import com.gwsoft.server.conf.ServerConf;
import com.gwsoft.server.net.tcp.DataHandler;

/**
 * @author 作者姓名 E-mail: email地址
 * @version 创建时间：2010-11-17 下午12:10:24
 * info
 */
public class ServerStart {

	static int PORT = 8086 ;
	
	static IServer server = null ;
	
	static{
		String portstr = ServerConf.getProperty(ServerConf.PORT) ;
		if(portstr == null){
			Logger.getLogger(ServerStart.class).error("server port error") ;
		}
		PORT = Integer.parseInt(portstr) ;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		try {
			server = new Server(PORT ,new DataHandler());
			
			server.start() ;
			System.out.println("server run ip " + server.getLocalAddress() +":"+PORT); 
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static boolean startServer(){
		try {
			
			server = new Server(PORT ,new DataHandler());
			server.setConnectionTimeoutMillis(999999999) ;
			server.start() ;
			System.out.println("server run ip " + server.getLocalAddress() +":"+PORT); 
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return false ;
		} catch (IOException e) {
			e.printStackTrace();
			return false ;
		}
		return true ;
	}
	
	public static void shutdownServer(){
		try {
			server.close() ;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
